//
//  FQDAllColumnsViewController.m
//  FakeQDaily
//
//  Created by mewxiv on 9/20/16.
//  Copyright © 2016 Xensius. All rights reserved.
//

#import "FQDAllColumnsViewController.h"
#import "FQDColumnArticleFeedViewController.h"
#import "FQDSubscribeManager.h"
#import "FQDRegisterNavViewController.h"
#import "FQDSimpleFeedCellItemProvider.h"
#import "FQDAccountManager.h"
#import "FQDRemindCell.h"
#import "FQDAllColumnsFeedCell.h"
#import "FQDColumnFeedCellItem.h"
#import "QDailyAPI.h"
#import "FQDRestModel.h"
#import "MBProgressHUD+FQDExtension.h"

static NSString *const kAllColumnsFeedCacheId = @"all_columns";

@interface FQDAllColumnsViewController ()

@end

@implementation FQDAllColumnsViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupCellItemProvider];
    [self setupNotifications];
    [self loadNewCellItemsWithCompletion:nil];
}

- (void)setupCellItemProvider
{
    FQDSimpleFeedCellItemProvider *provider = [[FQDSimpleFeedCellItemProvider alloc] init];
    provider.responseFeedClass = [FQDColumn class];
    provider.cellItemClass = [FQDColumnFeedCellItem class];
    provider.responseFeedsName = @"columns";
    [provider setFetchFeedBlock:^(NSString *feedId, NSString *cursorKey, QDailyAPICompleteBlock completion) {
        [[QDailyAPI sharedAPI] getAllColumnsInColumnCenter:cursorKey completion:completion];
    }];
    self.provider = provider;
}

- (void)setupNotifications
{
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(subscribeColumnsDidChange:)
                          name:FQDSubscribeColumnsDidChangeNotification object:nil];
}

#pragma mark - FQDFeedBaseViewController override
- (void)didSelectHeaderCellInCollectionView:(UICollectionView *)collectionView
{
    FQDRegisterNavViewController *registerVC = [[FQDRegisterNavViewController alloc] init];
    registerVC.backgroundViewController = self;
    [self presentViewController:registerVC animated:YES completion:nil];
}

- (void)didSelectCellItem:(FQDFeedCellItem *)cellItem atIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
{
    FQDColumnFeedCellItem *columnCellItem = (FQDColumnFeedCellItem *)cellItem;
    FQDColumn *column = columnCellItem.column;
    FQDColumnArticleFeedViewController *feedVC = [[FQDColumnArticleFeedViewController alloc] init];
    feedVC.column = column;
    [self.navigationController pushViewController:feedVC animated:YES];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForHeaderCellAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(collectionView.frame.size.width, 43.0f);
}

- (NSInteger)numberOfHeaderCellsInCollectionView:(UICollectionView *)collectionView
{
    BOOL isLogined = [[FQDAccountManager sharedManager] isLogined];
    return (isLogined ? 0 : 1);
}

- (void)configureFeedCell:(UICollectionViewCell *)feedCell atIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
{
    FQDAllColumnsFeedCell *cell = (FQDAllColumnsFeedCell *)feedCell;
    cell.delegate = self;
}

- (Class)cellClassForCellItem:(FQDFeedCellItem *)cellItem
{
    return [FQDAllColumnsFeedCell class];
}

- (NSArray *)cellClasses
{
    return @[
        [FQDAllColumnsFeedCell class],
        [FQDRemindCell class],
    ];
}

- (Class)headerCellClassAtIndexPath:(NSIndexPath *)indexPath inCollectionView:(UICollectionView *)collectionView
{
    return [FQDRemindCell class];
}

#pragma mark - FQDBaseCellActionDelegate
- (void)collectionViewCell:(FQDAllColumnsFeedCell *)cell actionType:(NSInteger)actionType actionControl:(UIControl *)control userInfo:(NSDictionary *)userInfo
{
    if (actionType == FQDAllColumnsFeedCellActionSubscribe) {
        FQDSubscribeManager *subscribeManager = [FQDSubscribeManager sharedManager];
        FQDColumnFeedCellItem *cellItem = (FQDColumnFeedCellItem *)cell.cellItem;
        [subscribeManager toggleSubscribeColumn:cellItem.column.ID
         completion:^(BOOL success, BOOL subscribed) {
             if (success) {
                 cell.subscribed = subscribed;
             }
         }];
    }
}

#pragma mark - Notifications
- (void)subscribeColumnsDidChange:(NSNotification *)note
{
    [self reloadData];
}

@end
